package com.d1708.zhanggm.springcloud.jpa.controller;

import com.d1708.zhanggm.springcloud.jpa.config.Log;
import com.d1708.zhanggm.springcloud.jpa.dao.OrderRepository;
import com.d1708.zhanggm.springcloud.jpa.dao.RoleRepository;
import com.d1708.zhanggm.springcloud.jpa.dao.UserRepository;
import com.d1708.zhanggm.springcloud.jpa.entity.OrderEntity;
import com.d1708.zhanggm.springcloud.jpa.entity.UserEntity;
import com.d1708.zhanggm.springcloud.jpa.model.PostDataModel;
import com.d1708.zhanggm.springcloud.jpa.service.AreaService;
import com.d1708.zhanggm.springcloud.jpa.service.OrderService;
import com.d1708.zhanggm.springcloud.jpa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Api(tags = {"JPA测试Controller"})
@RestController
@RequestMapping("/jpa/")
public class JpaController {

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AreaService areaService;

    /**
     * 分页接口
     */
    @ApiOperation(value = "用户分页列表接口",notes = "当前页和分页小默认值：1,10")
    @GetMapping("user")
    @Log("query")
    public Page<UserEntity> getPageInfo(UserEntity userEntity,@RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize){

        return  userService.getPageInfo(userEntity,pageNum,pageSize);
    }
    @GetMapping("order")
    public Page<OrderEntity> order(OrderEntity orderEntity, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return orderService.getPageInfo(orderEntity,pageNum,pageSize);
    }

    @GetMapping("role")
    public Object getRoleAll(){
        return roleRepository.findAll();
    }

    @GetMapping("getAreaTree")
    public Object getAreaTree(){
        return areaService.getTree();
    }

    //RequestBody接收的是Json数据：{}
    @PostMapping("postData")
    public Object postData(@RequestBody PostDataModel postDataModel){
        System.out.println(postDataModel);
        return areaService.getTree();
    }
}
